import mongoose from 'mongoose';
import md5 from 'blueimp-md5';

const userSchema = new mongoose.Schema({
    username: { type: String, required: true },
    password: { type: String, required: true },
    phone: String,
    email: String,
    create_time: { type: Number, default: Date.now },
    role_id: String
})

const UserModel = mongoose.model('user', userSchema);

// (async function createAdmin() {
//     let user = await UserModel.findOne({ username: 'admin' });
//     if (!user) {
//         try {
//             await UserModel.create({ username: 'admin', password: md5('admin') })
//             console.log(`初始化用户: \r\n\t用户名：admin 密码：admin`);
//         } catch (e) {
//             console.error(`初始化用户失败：${e.message}`);
//         }
//     }
// })()

UserModel.findOne({ username: 'admin' })
    .then(user => {
        if (!user) {
            UserModel.create({ username: 'admin', password: md5('admin') })
                .then(user => {
                    console.log(`初始化用户: \r\n\t用户名：admin 密码：admin`);
                })
        }
    })

export default UserModel;
